package com.dongchedi.service.impl;

import com.dongchedi.entity.CustomerLogin;
import com.dongchedi.entity.LoginTicket;
import com.dongchedi.mapper.CustomerInfoMapper;
import com.dongchedi.mapper.UserLoginMapper;
import com.dongchedi.service.FindLoginService;
import com.dongchedi.utils.CheckUtil;
import com.dongchedi.utils.HostHolder;
import com.dongchedi.utils.Md5;
import com.dongchedi.utils.constant.RedisConstantKey;
import com.dongchedi.utils.constant.TimeConstant;
import com.dongchedi.utils.page.PageRequest;
import com.dongchedi.utils.page.PageResult;
import com.dongchedi.utils.page.PageUtil;
import com.dongchedi.vo.LoginVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author leibidong
 * @description 用户登录，注销实现类
 * @date 2021/6/26
 */
@Service
public class FindLoginServiceImpl implements FindLoginService {
    @Autowired
    UserLoginMapper userLoginMapper;
    @Autowired
    CustomerInfoMapper customerInfoMapper;
    @Resource
    RedisTemplate<String,Object> redisTemplate;
    @Resource
    HostHolder hostHolder;
    public CustomerLogin queryByName(String name){
        return  userLoginMapper.queryByName(name);
    }

    @Override
    public PageResult query(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        List<CustomerLogin> list=userLoginMapper.query();
        PageResult pageResult= PageUtil.getPageResult(pageRequest,new PageInfo<CustomerLogin>(list));
        return  pageResult;
    }
    /*
     * @Author: leibidong
     * @Description: 判断用户输入账号密码是否合法，如果初次登录，就想注册，再登录。否则就判断密码是否正确
     * @Date: 2021/6/26 19:53
     * @Param: [loginVO]
     * @Return: java.util.Map<java.lang.String,java.lang.Integer>
     **/
    @Override
    public Map<String,Integer> login(LoginVO loginVO) {
        Map<String,Integer> map=new HashMap<>();
        if(!(CheckUtil.checkLoginName(loginVO.loginName)&&CheckUtil.checkPassword(loginVO.password))){
            map.put("id",-1);
            return map;
        }
        CustomerLogin customerLogin=userLoginMapper.queryByName(loginVO.loginName);
        if(customerLogin==null){
           String md5Password= Md5.md5(loginVO.password);
            Date date = new Date();
            Timestamp nousedate = new Timestamp(date.getTime());
            CustomerLogin customerLogin1=new CustomerLogin();
            customerLogin1.setCreateTime(nousedate);
            customerLogin1.setLoginName(loginVO.loginName);
            customerLogin1.setPassword(md5Password);
            Integer i=userLoginMapper.insertUser(customerLogin1);

            Integer integer=customerLogin1.getId();
            if(integer==0){
                map.put("id",0);
                return map;
            }else {
                map.put("id",integer);
                String url="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=670268967,1045820523&fm=26&gp=0.jpg";
                String name="懂车帝默认昵称"+customerLogin1.getLoginName();
                String desc="懂车帝默认签名"+customerLogin1.getLoginName();
                customerInfoMapper.initInsert(integer,name,url,desc,nousedate);
                userLoginMapper.updateStatus(integer,1);
                return map;
            }
        }else{
            String md5Password= Md5.md5(loginVO.password);
            if(customerLogin.getPassword().equals(md5Password)){
                Integer i=userLoginMapper.updateStatus(customerLogin.getId(),1);
                if(i==0){
                    map.put("id",0);
                    return map;
                }else{
                    map.put("id",customerLogin.getId());
                    return map;
                }
            }else{
                map.put("id",0);
                return map;
            }
        }
    }
    /*
     * @Author: leibidong
     * @Description: 判断用户是否在线，如果不在线，直接返回失败，如果在线，就注销登录
     * @Date: 2021/6/26 19:54
     * @Param: [id]
     * @Return: java.lang.Integer
     **/
    @Override
    public Integer loginout(Integer id,String ticket) {
        CustomerLogin customerLogin1=hostHolder.getUser();
        if(customerLogin1.getId().equals(id)) {
            CustomerLogin customerLogin = userLoginMapper.queryStatus(id);
            if (customerLogin.getStatus() == 0) {
                return 0;
            }
            String loginRedisKey= RedisConstantKey.getLoginRedisKey(ticket);
            redisTemplate.delete(loginRedisKey);
            return userLoginMapper.updateStatus(id, 0);
        }else
        {
            return 0;
        }
    }
    /*
     * @Author: leibidong
     * @Description: 根据ticket，获取用户登录状态
     * @Date: 2021/7/1 17:32
     * @Param: [ticket]
     * @Return: com.dongchedi.entity.LoginTicket
     **/
    @Override
    public LoginTicket findLoginTicket(String ticket) {
        String redisKey = RedisConstantKey.getLoginRedisKey(ticket);
        return (LoginTicket)redisTemplate.opsForValue().get(redisKey);
    }
    /*
     * @Author: leibidong
     * @Description: 根据用户id查询用户基本信息
     * @Date: 2021/7/1 17:36
     * @Param: [id]
     * @Return: com.dongchedi.entity.CustomerLogin
     **/
    @Override
    public CustomerLogin queryCustomerLoginById(Integer id) {
        String key=RedisConstantKey.getRedisByLoginid(id);
        CustomerLogin redisValue=(CustomerLogin) redisTemplate.opsForValue().get(key);
        if(redisValue!=null){
            return redisValue;
        }else{
            CustomerLogin customerLogin=userLoginMapper.queryById(id);
            redisTemplate.opsForValue().set(key,customerLogin, TimeConstant.ONE_DAY_EXPIRED_SECONDS.getTimes(), TimeUnit.SECONDS);
            return customerLogin;
        }
    }
}
